package com.keekus.common.web;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Map;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.keekus.common.message.MessagesSource;

@Controller
public class WelcomeController extends BaseController {
	@RequestMapping(value = { "/home" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView toHome(Model model) {
		MessageSourceAccessor a = MessagesSource.getAccessor();
		String test = a.getMessage("AuthByAdapterProvider.incorrectKey");
		return new ModelAndView("main", model.asMap());
	}

	@RequestMapping(value = { "/home/{userid}" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public String personnalHome(@PathVariable String userid, Map model) {
		model.put("username", userid);

		return "Hello.";
	}

	@RequestMapping({ "/something" })
	public ResponseEntity<String> handle(HttpEntity<byte[]> requestEntity) throws UnsupportedEncodingException {
		String requestHeader = requestEntity.getHeaders().getFirst("MyRequestHeader");
		byte[] requestBody = (byte[]) requestEntity.getBody();

		HttpHeaders responseHeaders = new HttpHeaders();
		MediaType mt = new MediaType("application", "json", Charset.forName("utf-8"));
		responseHeaders.setContentType(mt);
		responseHeaders.set("MyResponseHeader", "MyValue");
		return new ResponseEntity(MessagesSource.getAccessor().getMessage("BindAuthenticator.badCredentials"), responseHeaders,
				HttpStatus.CREATED);
	}

	@RequestMapping({ "/coding" })
	@ResponseBody
	public String coding(HttpEntity<byte[]> requestEntity) throws UnsupportedEncodingException {
		//this.logger.info("test Locale:" + LocaleContextHolder.getLocale());
		return MessagesSource.getAccessor().getMessage("BindAuthenticator.badCredentials");
	}
}